package com.java.controller;

import com.java.model.*;
import com.java.service.IndexHelpService;
import com.java.service.MediaAccountService;
import com.java.service.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping("/System")
public class SystemController {

    @Autowired
    private PlatformService platformService;

    @Autowired
    private MediaAccountService mediaAccountService;

    @Autowired
    private IndexHelpService indexHelpService;

    /**
     * 资源类型页面跳转
     * @return
     */
    @GetMapping("/resourceType")
    public String resourceType(){
        return "/system/resourceType";
    }

    /**
     * 资源类型页面查询
     * @param platform
     * @return
     */
    @PostMapping("/toResourceType")
    @ResponseBody
    public Result toResourceType(Platform platform){
        return platformService.list(platform);
    }

    /**
     * 跳转资源类型新增页面
     * @return
     */
    @GetMapping("/resourceTypeAdd")
    public String add() {
        return "/system/resourceTypeAdd";
    }
    /**
     * 资源类型新增
     * @param Platform
     * @return
     */
    @PostMapping("/resourceTypeSave")
    @ResponseBody
    public Result save(@RequestBody Platform Platform) {
        platformService.save(Platform);
        return Result.success();
    }

    /**
     * 资源类型编辑页面跳转
     * @return
     */
    @GetMapping("resourceTypeEdit")
    public String resourceTypeEdit(int id, Model model){
        Result result = platformService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("platform", result.getData());
        }
        return "/system/resourceTypeEdit";
    }

    /**
     * 资源类型编辑访问顾问
     * @param platform
     * @return
     */
    @PostMapping("/resourceTypeModify")
    @ResponseBody
    public Result remove(@RequestBody Platform platform) {
        return platformService.modify(platform);
    }

    /**
     * 资源类型的启用禁用
     * @return
     */
    @PostMapping("/platformStatus")
    @ResponseBody
    public Result platformStatus(int[] ids, String status) {
        return platformService.platformStatus(ids, status);
    }

    /**
     * 资源类型删除
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public Result removes(int[] ids) {
        return platformService.remove(ids);
    }

    /**
     * 资源分类页面跳转
     * @return
     */
    @GetMapping("/resourceClassification")
    public String resourceClassification(){
        return "/system/resourceClassification";
    }

    /**
     * 资源分类页面查询
     * @param mediaAccount
     * @return
     */
    @PostMapping("/toResourceClassification")
    @ResponseBody
    public Result toResourceClassification(MediaAccount mediaAccount){
        return mediaAccountService.resource(mediaAccount);
    }

    /**
     * 帮助中心页面跳转
     * @return
     */
    @GetMapping("/helpCenter")
    public String helpCenter(){
        return "/system/helpCenter";
    }

    /**
     * 帮助中心页面查询
     * @param indexHelp
     * @return
     */
    @PostMapping("/toHelpCenter")
    @ResponseBody
    public Result toHelpCenter(IndexHelp indexHelp){
        return indexHelpService.list(indexHelp);
    }

    /**
     * 富文本
     * @param request
     * @param file
     * @return
     */
    @RequestMapping("/file")
    @ResponseBody
    public Map<String,Object> felt(HttpServletRequest request, @RequestParam MultipartFile file) {

        Map<String,Object> mv=new HashMap<String, Object>();
        Map<String,String> mvv=new HashMap<String, String>();
        try {
            String rootPath = request.getSession().getServletContext().getRealPath("/image/");
            Calendar date = Calendar.getInstance(); //Calendar.getInstance()是获取一个Calendar对象并可以进行时间的计算，时区的指定
            String originalFile = file.getOriginalFilename(); //获得文件最初的路径
            String uuid = UUID.randomUUID().toString();    //UUID转化为String对象
            String newfilename=date.get(Calendar.YEAR)+""+(date.get(Calendar.MONTH)+1)+""+date.get(Calendar.DATE)+uuid.replace("-", "")+originalFile;
            //得到完整路径名
            File newFile = new File(rootPath+newfilename);
            /*文件不存在就创建*/
            if(!newFile.getParentFile().exists()){
                newFile.getParentFile().mkdirs();
            }
            String filename=originalFile.substring(0, originalFile.indexOf("."));
            System.out.println(originalFile);
            System.out.println(filename);
            file.transferTo(newFile);
            System.out.println("newFile : "+newFile);
            String urlpat= "/image/"+ newfilename;
            mvv.put("src", urlpat);
            mvv.put("title",newfilename);
            mv.put("code", 0);
            mv.put("msg", "上传成功");
            mv.put("data", mvv);
            return mv;
        } catch (Exception e) {
            e.printStackTrace();
            mv.put("success", 1);
            return mv;
        }
    }

    /**
     * 帮助中心新增页面跳转
     * @return
     */
    @GetMapping("helpAdd")
    public String helpCenterAdd(){
        return "/system/helpAdd";
    }


    /**
     * 跳转到帮助中心编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/helpView")
    public String view(int id, Model model) {
        Result result = indexHelpService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("indexHelp", result.getData());
        }

        return "/system/helpView";
    }

    /**
     * 帮助中心新增
     * @param indexHelp
     * @return
     */
    @PostMapping("/helpSave")
    @ResponseBody
    public Result save(IndexHelp indexHelp) {
        indexHelp.setTitie(indexHelp.getTitie());
        indexHelp.setContent(indexHelp.getContent()); // 内容
        indexHelp.setCreateTime(new Date()); // 当前时间
        indexHelp.setStatus(1);
        indexHelpService.save(indexHelp);
        return Result.success();
    }

    /**
     * 跳转到帮助中心编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/helpEdit")
    public String edit(int id, Model model) {
        Result result = indexHelpService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("indexHelp", result.getData());
        }

        return "/system/helpEdit";
    }
    /**
     * 帮助中心编辑访问顾问
     * @param indexHelp
     * @return
     */
    @PostMapping("/helpModify")
    @ResponseBody
    public Result remove(IndexHelp indexHelp) {
        indexHelp.setTitie(indexHelp.getTitie());
        indexHelp.setContent(indexHelp.getContent());
        indexHelp.setCreateTime(new Date());
        indexHelp.setStatus(1);
        indexHelpService.modify(indexHelp);
        return Result.success();
    }


    /**
     * 帮助中心删除
     * @param ids
     * @return
     */
    @PostMapping("/helpRemove")
    @ResponseBody
    public Result remove(int[] ids) {
        return indexHelpService.remove(ids);
    }


}
